package io;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: ycy
 * @CreateDate: 2019/3/27 11:03
 */
public class Analyzer {

  public static void main(String[] args) {
    Analyzer analyzer = new Analyzer();
    analyzer.getString();
  }

  public void getString() {
    List<R> idNameList = Lists.newLinkedList();
    File file = new File("D:\\Users\\yincy\\Desktop\\9885124.csv");
    try {
      FileInputStream fis = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
      BufferedReader br = new BufferedReader(isr);
      Map<String, Integer> countMap = Maps.newHashMap();
      Map<String, String> idNameMap = Maps.newTreeMap();

      while (br.ready()) {
        String s = br.readLine();
        String[] split = s.split("@@@");
        String id = split[0];
        String name = split[1];
        Set<String> set = Sets.newTreeSet();
        if (StringUtils.isNotBlank(name)) {
//          idNameMap.put(id, test2(name));
//          System.out.println(id + "@@@" + test2(name));
          idNameList.add(new R(id, test2(name)));
        }
      }
//      for (Map.Entry<String, String> stringIntegerEntry : idNameMap.entrySet()) {
//        System.out.println(stringIntegerEntry.getKey() + "@@" + stringIntegerEntry.getValue());
//      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
    }

    printOutFile(idNameList);

  }

  class R {
    String id;
    String name;

    public R(String id, String name) {
      this.id = id;
      this.name = name;
    }
  }

  public String test2(String str) {
    //第一步过滤
    Pattern compileFilter =
            Pattern.compile("不可单独预订|不含|\\d+周?岁及?以上|\\d+周?岁及?以下|\\d+([-~]+\\d+)?周?岁|" +
                    "\\d+日?天|(平日|周末|周日|周六|周五|周四|周三|周二|周一)|" +
                    "联票|全票|三人票|门票|成人票|儿童票|家庭票|双人票|大?学生票|老人票|优待票|亲子票|团队票|残疾票|单程票|普通票|电子票|青年票|通票|" +
                    "((老人|学生|记者|儿童|军人|现役军人|武警|残疾人|教师)[/、])+(老人|学生|记者|儿童|军人|现役军人|武警|残疾人|教师)票?|" +
                    "不含餐|[A-Za-z]+区|[A-Za-z]+套|[A-Za-z]+档座票?|自动发码|即买即用|随时可退|" +
                    "\\d+大\\d+小|\\d{1,2}[：:]{1}\\d{1,2}([-~]+\\d{1,2}[：:]{1}\\d{1,2})?(场次)?(场)?(班次)?(间班次)?|\"");
    str = compileFilter.matcher(str).replaceAll("");
    str = str.replaceAll("\\([/,，\\s\\w-~]*\\)", "").replaceAll("【[/,，\\s\\w-~]*】", "").replaceAll("（[/,，\\s\\w-~]*）", "");
    //过滤最后一个(.....)
    //第二步 只取第一个括号里面的内容
    Pattern compile = Pattern.compile("(\\(.*?\\))|(（.*?）)");
    Matcher matcher = compile.matcher(str);
    //舍去最后一个括号里面的内容
    List<String> contents = Lists.newLinkedList();
    int parenthesesCount = 0;
    int lastLeftIndex = 0;
      while (matcher.find()) {
        parenthesesCount ++;
        contents.add(matcher.group());
        lastLeftIndex = matcher.start();
      }
    if (parenthesesCount > 1) {
      str = str.substring(0, lastLeftIndex);
    }
    return str;
  }

  public void printOutFile(List<R> idNameList) {
    Map<Integer, Set<String>> idNameMap = Maps.newHashMap();
    for (R r : idNameList) {
      Integer id = Integer.parseInt(r.id);
      Set<String> strings = idNameMap.get(id);
      if (StringUtils.isBlank(r.name)) continue;
      if (strings == null) {
        strings = Sets.newTreeSet();
        strings.add(r.name);
      } else {
        strings.add(r.name);
      }
      idNameMap.put(id, strings);
    }
    //打印
    for (Map.Entry<Integer, Set<String>> integerSetEntry : idNameMap.entrySet()) {
      Integer key = integerSetEntry.getKey();
      Set<String> value = integerSetEntry.getValue();
      String name = StringUtils.join(value.iterator(), ",");
      System.out.println(key + "@@@@" + name);
    }

    try {
      PrintWriter pw = new PrintWriter(new PrintStream(new FileOutputStream("\\Users\\yincy\\Desktop\\1111.txt")));
      for (R alias : idNameList) {
        pw.println(alias.id + "@@@" + alias.name);
      }
      pw.flush();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {

    }
  }

}
